import { useMemo, useState } from 'react';
import dayJs from 'dayjs';
import { useRequest } from 'ahooks';
// import { useWebsocketPush } from '@/common/hooks';
// import { getMatchList } from '@/service/sports';
// import useScoreState from './useScoreState';
import { getFootballMatchList, getBasketballMatchList } from '@/service/sports';
import { scoreDataFormat } from '@/common/format';
import DefaultFootballMatchImage from '@/assets/images/default_football_match_image.png';
import DefaultBasketballMatchImage from '@/assets/images/default_basketball_match_image.png';
// import { basketballMessage, footballMessage } from './websocketMessage';

const WEEK_MAP = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
const useScorePageData = ({ sportId }) => {
  const [list, setList] = useState([]);
  const todayDayIns = dayJs();

  const getMatchList = useMemo(() => ({
    1: getFootballMatchList,
    2: getBasketballMatchList,
  }[sportId]), [sportId]);

  const { loading } = useRequest(
    () => getMatchList({ pageNum: 1, pageSize: 999, type: 1, date: +todayDayIns }),
    {
      defaultLoading: true,
      onSuccess: (d) => setList((d || []).map(scoreDataFormat)),
    },
  );

  const { isFootball, isBasketball, defaultMatchImage } = useMemo(() => {
    let defaultImage;
    const isFootballFlag = /^1$/.test(sportId);
    const isBasketballFlag = /^2$/.test(sportId);
    if (isFootballFlag) {
      defaultImage = DefaultFootballMatchImage;
    }
    if (isBasketballFlag) {
      defaultImage = DefaultBasketballMatchImage;
    }

    return {
      isFootball: isFootballFlag,
      isBasketball: isBasketballFlag,
      defaultMatchImage: defaultImage,
    };
  }, [sportId]);

  // const { updateData: { messageType, list: updateList } } = useScoreState();
  // const handleUpdateList = useCallback(() => {
  //   const formatUpdateList = updateList.map(scoreUpdateDataFormat);
  //   const code = { 1: 100018, 2: 100019 }[sportId];
  //   // 比赛类型符合
  //   const isCode = messageType === code;
  //   // 进行中的才需要
  //   // const isProgress = type === enumScheduleType.progress;
  //   const isRefresh = isCode && formatUpdateList?.length;
  //   if (isRefresh) {
  //     // 浅拷贝 改变代码结构
  //     const res = [...list];
  //     // 取出返回数据的所有id
  //     const ids = formatUpdateList.map(({ id }) => id);
  //     // 过滤符合条件的列表数据
  //     const ableList = res.filter(({ id }) => ids.includes(id));

  //     formatUpdateList.forEach((item) => {
  //       // 遍历筛选出来的数据
  //       const index = ableList.findIndex((_) => item.id === _.id);
  //       if (index > -1) {
  //         ableList[index] = Object.assign(ableList[index], item);
  //       }
  //     });
  //     return res;
  //   }
  //   return [];
  // }, [sportId, messageType, list, updateList]);

  // useEffect(() => {
  //   if (updateList?.length) {
  //     const data = handleUpdateList();
  //     if (data?.length) setList(data);
  //   }
  // }, [messageType, updateList]);

  return {
    loading,
    list,
    todayLabel: todayDayIns.format(`MM月DD日 ${WEEK_MAP[todayDayIns.day()]}（今天）`),
    isFootball,
    isBasketball,
    defaultMatchImage,
  };
};

export default useScorePageData;
